gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\linear\finite\perceptr.m

    function [alpha,theta,solution,t]=perceptr(X,J,tmax,t,alpha,theta)
% PERCEPTR Perceptron learning rule searching for decision hyperplane. 
% [alpha,theta,solution,t]=perceptr(X,J,tmax,t,alpha,theta)
%
% PERCEPTR is implementation of Perceptron learning rule. This
%  algorithm finds vector alpha and threshold theta which solve
%  following task
%          alpha' * x >= theta, for x=X(:,i), J(i)=1 (first class)
%          alpha' * x < theta,  for x=X(:,i), J(i)=2 (second class)
%
%  Found alpha and theta determine decision hyperplane in
%  the D-dimensional feature space.
%
%  Perceptron algorithm works iteratively and if input classes
%  are linearly separable then it stops in finite number of steps.
%  This implementation allows to limit maximal number of steps and
%  allows to start algorithm from defined state determined by 
%  given alpha and theta.
%
%  Input
%   X [DxM] contains M training points in the D-dimensional feature 
%      space, i.e. X=[x1,x2,..XM] where xi is i-th column vectors.
%   J [1xM] contains class labels of the points in X. A class label 
%      has to be 1 for the first class or 2 for the second class.
%   tmax [1x1] 1. if is integer tmax > 0 then it determines a maximal 
%      number of algorithm steps, i.e. if the solution is not found 
%      until tmax-th step the algorithm will exit and set solution = 0.
%      2. if tmax==-1, then the algorithm only returns, in the variable
%      alpha, badly classified point which would have been used in
%      the adaptation step but the adaptation is not performed.
%   t [1x1], alpha [Dx1], theta [1x1] if these arguments enter function
%      then the algorithm begins from the state which they determine.
%
%  Output
%   alpha [Dx1] found normal vector of the hyperplane or, if tmax==-1,
%      badly classified point.
%   theta [1x1] found threshold of the hyperplane.
%   solution [1x1], 1 ... solution is found,
%                   0 ... solution is not found.
%   t [1x1] step number where the algorithm stoped in.
%
% See also KOZINEC, EKOZINEC, LINSVM.
%

% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz
% Written Vojtech Franc (diploma thesis) 01.11.1999, 13.4.2000
% Modifications
% 24. 6.00 V. Hlavac, comments polished.
% 15-dec-2000, texts, returns bad point

% Process input arguments.
if nargin < 3
   tmax=inf;
end
if nargin < 4,
   t=0;
   alpha=0;
   theta=0;
end
 
% Transform original feature space into the homogenous (theta=0)
% coordinates.
[alpha,X]=ctransf(alpha,theta,X,J);

N=size(X,1);            % dimension
K=size(X,2);            % number of training points

% Step 0.
if t==0,
   alpha=zeros(N,1);
   theta=0;
   t=1;
   tmax=tmax-1;
end

% It returns only the first badly classified point which 
% would have been used for updating solution.
% ----------------------------------------------------
if tmax == -1,

   % Search for any  x from X that alpha*x <= 0
   for i=1:K,
      % get one x from set X
      x=X(:,i);

      if alpha'*x <= 0,

        if J(i)==1,
          alpha = x(1:(end-1));
        else
          alpha = -x(1:(end-1));
        end
        
        solution = 0;
        return;
      end 
   end % for i=1:K,
   
   solution=1;
   return;

else
% Iterate until solution is not found or
% number of steps exceeds given limit tmax.
% -------------------------------------------------     
   solution = 0;
   while solution == 0 & tmax > 0,
      tmax = tmax-1;
      
      % It's apriory supposed that the solution is found.
      solution = 1;

      % Search for any  x from X that alpha*x <= 0
      for i=1:K,
         % get one x from set X
         x=X(:,i);

         % check x
         if alpha'*x <= 0,

            % Adjust alpha
            alpha=alpha+x;

            % Increase step number
            t=t+1;
            solution = 0;
            break;
         end % if

      end % for i=1:K,

   end % while solution ==0, ...
end

% Transform the found solution from the homogenous coordinates
% into original space.
[alpha,theta]=ictransf(alpha);